package com.kk.permission.utils.callershow;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.util.Log;

import com.kk.R;
import com.kk.permission.GuideConst;
import com.kk.permission.IPermissionGuideStrategy;
import com.kk.permission.utils.BaseUtil;
import com.kk.permission.utils.OSUtil;
import com.kk.permission.utils.PackageUtil;
import com.kk.permission.utils.PrefUtil;
import com.kk.permission.utils.floatwindow.FloatWindowPermissionCheck;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class CallerShowUtils {
    public static String IS_OPEN_HALF_AUTO_ACTIVITY_TAG = "is_open_half_auto_activity_tag";
    public static String KEY_CHECK_FLOATING_WINDOW_PERMISSION_IS_USE_SP_TAG = "key_check_floating_window_permission_is_use_sp_tag";
    public static String KEY_IS_USE_HALF_AUTO_TAG = "key_is_use_half_auto_tag";
    public static String KEY_OPEN_ACCESSIBILITY_SETTING_SP_TAG = "key_open_accessibility_setting_sp_tag";
    private static final String TAG = "CallerShowUtils";
    private static boolean isCanAuto;
    //    private static HalfAutoPermissionCheckDialog mDialog;
    private static long startAutoWaitProgress;

    public static void tagOpenAccessibitySetting(Context context, boolean z) {
        CallerShowSPUtils.setParam(context, KEY_OPEN_ACCESSIBILITY_SETTING_SP_TAG, Boolean.valueOf(z));
    }

    public static void recordStartAutoWaitProgressTime() {
        startAutoWaitProgress = getTimeStamp();
    }


    public static void setIsCanAuto(boolean isCanAuto1){
        isCanAuto = isCanAuto1;
    }

    private static Boolean isCanAuto()
    {
        return PackageUtil.queryAutoPermission();
    }

    public static boolean checkSysModifyPermission(Context context) {
        boolean z = false;
        boolean keyBoolean = PrefUtil.getKeyBoolean("setted_call_ringtone_permission", false);
        if (isLowAndroidVersion6()) {
            z = true;
        } else {

        }
        if (!z) {
            z = getHalfAutoPermissionDoneTag(GuideConst.CALL_RINGTONE_PERMISSION);
        }
        if (!z) {
            z = PrefUtil.getKeyBoolean("done_setted_call_ringtone_permission", false);
        }
        boolean halfAutoPermissionDoneTag = getHalfAutoPermissionDoneTag(GuideConst.CALL_RINGTONE_PERMISSION);
        setCheckResult(GuideConst.CALL_RINGTONE_PERMISSION, keyBoolean, z);
        if (z || halfAutoPermissionDoneTag) {
            return true;
        }
        return false;
    }

    public static boolean isLowAndroidVersion6() {
        return Build.VERSION.SDK_INT < 23;
    }

    public static boolean getHalfAutoPermissionDoneTag(String str) {
        Context appContext = BaseUtil.getAppContext();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(IPermissionGuideStrategy.PREF_KEY_HALF_AUTO_DONE_PREFIX);
        stringBuilder.append(str);
        return ((Boolean) CallerShowSPUtils.getParam(appContext, stringBuilder.toString(), Boolean.valueOf(false))).booleanValue();
    }

    public static void setFloatingWindowPermissionDefaultTag() {
        CallerShowSPUtils.setParam(BaseUtil.getAppContext(), "setted_toast_permission", Boolean.valueOf(true));
    }

    private static void setCheckResult(String str, boolean z, boolean z2) {
        Map hashMap = new HashMap();
        hashMap.put("key_specific_permission_result", str);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(z);
        stringBuilder.append("_");
        stringBuilder.append(z2);
        hashMap.put("checkresult", stringBuilder.toString());
        stringBuilder = new StringBuilder();
        stringBuilder.append(str);
        stringBuilder.append(":");
        stringBuilder.append(z);
        stringBuilder.append("_");
        stringBuilder.append(z2);
    }

    public static boolean checkAutoBootPermission() {
        if (!isCanAuto) {
            return getHalfAutoPermissionDoneTag(GuideConst.AUTOBOOT_PERMISSION);
        }
        boolean z = false;
        boolean keyBoolean = PrefUtil.getKeyBoolean("setted_autoboot_permission", false);
        boolean keyBoolean2 = PrefUtil.getKeyBoolean("done_setted_autoboot_permission", false);
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(keyBoolean2);
        stringBuilder.append("  ");
        stringBuilder.append(keyBoolean);
        Log.e("yyy", stringBuilder.toString());
        Object obj = (OSUtil.isHuaweiAndroid9() && (((Boolean) CallerShowSPUtils.getParam(BaseUtil.getAppContext(), "setted_autoboot_permission", Boolean.valueOf(false))).booleanValue() || ((Boolean) CallerShowSPUtils.getParam(BaseUtil.getAppContext(), "done_setted_autoboot_permission", Boolean.valueOf(false))).booleanValue())) ? 1 : null;
        Object obj2 = (OSUtil.isHuaweiAndroid8() && (((Boolean) CallerShowSPUtils.getParam(BaseUtil.getAppContext(), "setted_autoboot_permission", Boolean.valueOf(false))).booleanValue() || ((Boolean) CallerShowSPUtils.getParam(BaseUtil.getAppContext(), "done_setted_autoboot_permission", Boolean.valueOf(false))).booleanValue())) ? 1 : null;
        Object obj3 = (OSUtil.isHuaweiAndroid6() && (((Boolean) CallerShowSPUtils.getParam(BaseUtil.getAppContext(), "setted_autoboot_permission", Boolean.valueOf(false))).booleanValue() || ((Boolean) CallerShowSPUtils.getParam(BaseUtil.getAppContext(), "done_setted_autoboot_permission", Boolean.valueOf(false))).booleanValue())) ? 1 : null;
        setCheckResult(GuideConst.AUTOBOOT_PERMISSION, keyBoolean, keyBoolean2);
        if (!(!keyBoolean && !keyBoolean2 && obj == null && obj2 == null && obj3 == null)) {
            z = true;
        }
        return z;
    }

    public static boolean checkBackgroundProtectPermission() {
        if (!isCanAuto) {
            return getHalfAutoPermissionDoneTag(GuideConst.BACKGROUND_PROTECT_PERMISSION);
        }
        boolean z = false;
        boolean keyBoolean = PrefUtil.getKeyBoolean("done_setted_half_autobackground_protect_permission_lock", false);
        boolean keyBoolean2 = PrefUtil.getKeyBoolean("setted_background_protect_permission_lock", false);
        boolean keyBoolean3 = PrefUtil.getKeyBoolean("done_setted_background_protect_permission_lock", false);
        setCheckResult(GuideConst.BACKGROUND_PROTECT_PERMISSION, keyBoolean2, keyBoolean3);
        if (keyBoolean2 || keyBoolean3 || keyBoolean) {
            z = true;
        }
        return z;
    }

    public static boolean checkMiuiDisallowPowerOptimizationPermission() {
        if (!isCanAuto) {
            return getHalfAutoPermissionDoneTag(GuideConst.DONT_OPTIMIZE_POWER);
        }
        boolean z = false;
        boolean keyBoolean = PrefUtil.getKeyBoolean("setted_don_not_optimize_power", false);
        boolean keyBoolean2 = PrefUtil.getKeyBoolean("done_setted_don_not_optimize_power", false);
        if (keyBoolean || keyBoolean2) {
            z = true;
        }
        return z;
    }

    public static boolean checkShowInLockScreenPermission() {
        if (!isCanAuto) {
            return getHalfAutoPermissionDoneTag(GuideConst.SHOW_IN_LOCKSCREEN_PERMISSION);
        }
        boolean z = false;
        boolean keyBoolean = PrefUtil.getKeyBoolean("setted_show_in_lockscreen_permission", false);
        boolean keyBoolean2 = PrefUtil.getKeyBoolean("done_setted_show_in_lockscreen_permission", false);
        setCheckResult(GuideConst.SHOW_IN_LOCKSCREEN_PERMISSION, keyBoolean, keyBoolean2);
        if (keyBoolean || keyBoolean2) {
            z = true;
        }
        return z;
    }

    public static boolean checkBackShowPermission() {
        if (!isCanAuto) {
            return getHalfAutoPermissionDoneTag(GuideConst.BACK_SHOW_PERMISSION);
        }
        boolean z = false;
        boolean keyBoolean = PrefUtil.getKeyBoolean("setted_back_show_permission", false);
        boolean keyBoolean2 = PrefUtil.getKeyBoolean("done_setted_back_show_permission", false);
        if (keyBoolean || keyBoolean2) {
            z = true;
        }
        return z;
    }

    public static boolean checkAllowNotiPermission() {
        if (!isCanAuto) {
            return getHalfAutoPermissionDoneTag(GuideConst.ALLOW_NOTI_PERMISSION);
        }
        boolean z = false;
        boolean keyBoolean = PrefUtil.getKeyBoolean("done_setted_allow_noti_permission", false);
        setCheckResult(GuideConst.ALLOW_NOTI_PERMISSION, false, keyBoolean);
        if (keyBoolean) {
            z = true;
        }
        return z;
    }

    public static boolean checkSystemDialingPermssion() {
        boolean keyBoolean = PrefUtil.getKeyBoolean("setted_system_dialing_permission", false);
        boolean keyBoolean2 = PrefUtil.getKeyBoolean("done_setted_system_dialing_permission", false);
        setCheckResult(GuideConst.SYSTEM_DIALING_PERMISSION, keyBoolean, keyBoolean2);
        if (keyBoolean || keyBoolean2) {
            return true;
        }
        return false;
    }

    public static boolean checkTurstApplicationPermission() {

        if (!isCanAuto) {
            return getHalfAutoPermissionDoneTag(GuideConst.TRUST_APPLICATION_PERMISSION);
        }
        boolean z = false;
        boolean keyBoolean = PrefUtil.getKeyBoolean("setted_background_protect_permission", false);
        boolean keyBoolean2 = PrefUtil.getKeyBoolean("done_setted_background_protect_permission", false);
        if (keyBoolean || keyBoolean2) {
            z = true;
        }
        return z;
    }

    public static boolean checkNotificationSettingPermission(Context context) {
        String packageName = context.getPackageName();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("checkNotificationSettingPermission pkgName is : ");
        stringBuilder.append(packageName);
        Settings.Secure.getString(context.getContentResolver(), "enabled_notification_listeners");
        boolean keyBoolean = PrefUtil.getKeyBoolean("setted_dial_noti_permission", false);
        boolean keyBoolean2 = PrefUtil.getKeyBoolean("done_setted_dial_noti_permission", false);
        boolean booleanValue = ((Boolean) CallerShowSPUtils.getParam(context.getApplicationContext(), "done_setted_half_autodial_noti_permission", Boolean.valueOf(false))).booleanValue() | keyBoolean2;
        setCheckResult(GuideConst.DIAL_NOTI_PERMISSION, keyBoolean, keyBoolean2);
        return booleanValue;
    }

    public static boolean checkFloatingWindowPermission(Context context) {
        if (!isCanAuto) {
            return getHalfAutoPermissionDoneTag(GuideConst.TOAST_PERMISSION);
        }
        boolean z = true;

        boolean keyBoolean;
        boolean keyBoolean2;
        if (OSUtil.isVivoX20Android7()
                || OSUtil.isXiaoMixAndroid8()
                || OSUtil.isVivoY85AAndroid8()
                || OSUtil.isOppoX7Aandroid6()
                || OSUtil.isVivoX9iAndroid7()
                || OSUtil.isRedMiNote4XAndroid7()
                || OSUtil.isJianGuoPro2s()
//                || PackageUtil.isPackageInstalled(PackageUtil.OPPO_PERMISSION_SETTING_PACKAGE_NAMES)
                || OSUtil.isXiaoMi5sPlusAndroid8()
                || ((OSUtil.isVivo() && OSUtil.sdk23())
                || OSUtil.isRedmiNote2SDK21()
                || OSUtil.isMi4cSDKAndroid7())) {
            CallerShowSPUtils.setParam(context, KEY_CHECK_FLOATING_WINDOW_PERMISSION_IS_USE_SP_TAG, Boolean.valueOf(true));
            keyBoolean = PrefUtil.getKeyBoolean("setted_toast_permission", false);
            keyBoolean2 = PrefUtil.getKeyBoolean("done_setted_toast_permission", false);
            boolean halfAutoPermissionDoneTag = getHalfAutoPermissionDoneTag(GuideConst.TOAST_PERMISSION);
            setCheckResult(GuideConst.TOAST_PERMISSION, keyBoolean, keyBoolean2);
            if (!(keyBoolean || keyBoolean2 || halfAutoPermissionDoneTag || getFloatingWindowPermissionDefaultTag())) {
                z = false;
            }
            return z;
        }/* else if (OSUtil.isVivo()) {
            return VivoPermissionUtils.isFloatPermissionOpen(context);
        }*/ else {
            try {
                return new FloatWindowPermissionCheck().checkPermission(context);
            } catch (Exception unused) {
                CallerShowSPUtils.setParam(context, KEY_CHECK_FLOATING_WINDOW_PERMISSION_IS_USE_SP_TAG, Boolean.valueOf(true));
                keyBoolean = PrefUtil.getKeyBoolean("setted_toast_permission", false);
                keyBoolean2 = PrefUtil.getKeyBoolean("done_setted_toast_permission", false);
                setCheckResult(GuideConst.TOAST_PERMISSION, keyBoolean, keyBoolean2);
                if (!(keyBoolean || keyBoolean2 || getFloatingWindowPermissionDefaultTag())) {
                    z = false;
                }
                return z;
            }
        }
    }

    public static void recordAutoWaitProgressStayTime() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("record key:key_auto_wait_progress_stay_time time = ");
        stringBuilder.append((getTimeStamp() - startAutoWaitProgress) / 1000);
        stringBuilder.append("s");
    }

    private static long getTimeStamp() {
        return java.lang.System.currentTimeMillis();
    }

    public static boolean checkBackgroundFrozenPermission() {
        if (!isCanAuto) {
            return getHalfAutoPermissionDoneTag(GuideConst.BACKGROUND_FROZEN_PERMISSION);
        }
        boolean z = false;
        boolean keyBoolean = PrefUtil.getKeyBoolean("setted_background_frozen_permission", false);
        boolean keyBoolean2 = PrefUtil.getKeyBoolean("done_setted_background_frozen_permission", false);
        setCheckResult(GuideConst.BACKGROUND_FROZEN_PERMISSION, keyBoolean, keyBoolean2);
        if (keyBoolean || keyBoolean2) {
            z = true;
        }
        return z;
    }

    private static boolean getFloatingWindowPermissionDefaultTag() {
        return ((Boolean) CallerShowSPUtils.getParam(BaseUtil.getAppContext(), "setted_toast_permission", Boolean.valueOf(false))).booleanValue();
    }

    public static void checkAllPermissonToShow(Context context, List<String> list) {
        if (list != null && list.size() > 0) {
            Iterator it = list.iterator();
            while (it.hasNext()) {
                String str = (String) it.next();
                if (str.equals(GuideConst.TOAST_PERMISSION) && checkFloatingWindowPermission(context)) {
                    it.remove();
                }
                if (str.equals(GuideConst.DIAL_NOTI_PERMISSION) && checkNotificationSettingPermission(context)) {
                    it.remove();
                }
                if (str.equals(GuideConst.AUTOBOOT_PERMISSION) && checkAutoBootPermission()) {
                    it.remove();
                }
                if (str.equals(GuideConst.CALL_RINGTONE_PERMISSION) && checkSysModifyPermission(context)) {
                    it.remove();
                }
                if (str.equals(GuideConst.SHOW_IN_LOCKSCREEN_PERMISSION) && checkShowInLockScreenPermission()) {
                    it.remove();
                }
                if (str.equals(GuideConst.BACKGROUND_PROTECT_PERMISSION) && checkBackgroundProtectPermission()) {
                    it.remove();
                }
                if (str.equals(GuideConst.DONT_OPTIMIZE_POWER) && checkMiuiDisallowPowerOptimizationPermission()) {
                    it.remove();
                }
                if (str.equals(GuideConst.INSTALL_SHORT_CUT) && checkInstallShortCutPermission()) {
                    it.remove();
                }
                if (str.equals(GuideConst.ALLOW_NOTI_PERMISSION) && checkAllowNotiPermission()) {
                    it.remove();
                }
                if (str.equals(GuideConst.TRUST_APPLICATION_PERMISSION) && checkTurstApplicationPermission()) {
                    it.remove();
                }
                if (str.equals(GuideConst.BACKGROUND_FROZEN_PERMISSION) && checkBackgroundFrozenPermission()) {
                    it.remove();
                }
                if (str.equals(GuideConst.SYSTEM_DIALING_PERMISSION) && checkSystemDialingPermssion()) {
                    it.remove();
                }
            }
        }
    }

    public static boolean checkInstallShortCutPermission() {
        if (!isCanAuto) {
            return getHalfAutoPermissionDoneTag(GuideConst.INSTALL_SHORT_CUT);
        }
        boolean z = false;
        boolean keyBoolean = PrefUtil.getKeyBoolean("setted_install_short_cut", false);
        boolean keyBoolean2 = PrefUtil.getKeyBoolean("done_setted_install_short_cut", false);
        setCheckResult(GuideConst.INSTALL_SHORT_CUT, keyBoolean, keyBoolean2);
        if (keyBoolean || keyBoolean2) {
            z = true;
        }
        return z;
    }

    @SuppressLint("WrongConstant")
    public static void jumpToSysMidifySettingActivity(Context context) {
        Intent intent;
        if (OSUtil.isRedMiNote4Android6() || OSUtil.isRedMi4Android6() || OSUtil.isXiaoMi8SEAndroid8()) {
            intent = new Intent();
            intent.putExtra("extra_pkgname", context.getPackageName());
            intent.setClassName("com.miui.securitycenter", "com.miui.permcenter.permissions.PermissionsEditorActivity");
            context.startActivity(intent);
            intent = null;
        } else {
            intent = new Intent("android.settings.action.MANAGE_WRITE_SETTINGS");
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("package:");
            stringBuilder.append(context.getPackageName());
            intent.setData(Uri.parse(stringBuilder.toString()));
        }
        if (isNeedClearTashTag()) {
            intent.setFlags(268435456);
            intent.addFlags(32768);
        }
        context.startActivity(intent);
    }

    public static boolean isNeedClearTashTag()
    {
        if (queryAutoPermission()) {
            return getHalAutoActivityOpenStatus() ^ true;
        }
        return false;
    }

    public static boolean queryAutoPermission() {
        return isCanAuto;
    }

    private static boolean getHalAutoActivityOpenStatus() {
        return ((Boolean) CallerShowSPUtils.getParam(BaseUtil.getAppContext(), IS_OPEN_HALF_AUTO_ACTIVITY_TAG, Boolean.valueOf(false))).booleanValue();
    }

    public static ArrayList<String> getPermissionListName(Context context, List<String> PermissionList){
        ArrayList<String> arrayList = new ArrayList<>();
        for(String permissionKey : PermissionList){
            switch (permissionKey){
                case GuideConst.TOAST_PERMISSION:
                    arrayList.add(context.getString(R.string.floating_windows));
                    break;
                case GuideConst.CALL_RINGTONE_PERMISSION:
                    arrayList.add(context.getString(R.string.hai_ringtone_permission));
                    break;
                case GuideConst.DIAL_NOTI_PERMISSION:
                    arrayList.add(context.getString(R.string.permission_open_notification));
                    break;
                case GuideConst.AUTOBOOT_PERMISSION:
                    arrayList.add(context.getString(R.string.miui5_autoboot_permission_hint));
                    break;
                case GuideConst.INSTALL_SHORT_CUT:
                    arrayList.add(context.getString(R.string.accessiblity_permission_huawei_shortcut));
                    break;
                case GuideConst.BACKGROUND_PROTECT_PERMISSION:
                    arrayList.add(context.getString(R.string.background_protect_permission_main));
                    break;
                case GuideConst.SYSTEM_DIALING_PERMISSION:
                    arrayList.add(context.getString(R.string.system_dialing_permission));
                    break;
            }
        }
        return arrayList;

    }
}
